package com.yuqian;


import com.google.gson.Gson;
import com.yuqian.Resposoyory.OffOnlineMessageReponsitory;
import com.yuqian.Service.OffOnline;
import com.yuqian.bean.Message;
import com.yuqian.bean.OffOnlineMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2017/7/31 0031.
 */
@Service
@RestController
public class OffOnlineMessageRest {

    Gson gson = new Gson();
    @Autowired
    OffOnline offOnline;

    @Autowired
    private OffOnlineMessageReponsitory reponsitory;

    @GetMapping(value = "/officeonline/{userid}", produces = "text/plan;charset=UTF-8")
    public String getOffOnLineMessage(@PathVariable("userid") String userId) {
        //   List<OffOnlineMessage> messageList=reponsitory.findOfficeOnlineMessagesByTowhereAndState(userId,"0");


        return null;//gson.toJson(messageList);
    }


    @GetMapping("/test")
    public String test() {
        OffOnlineMessage offOnlineMessage = new OffOnlineMessage();
        offOnlineMessage.setUserId("1");
        offOnlineMessage.setState("0");
        offOnlineMessage.setContent("测试");
        offOnlineMessage.setMessageType("message");
        offOnlineMessage.settowhere("2");
        offOnlineMessage.setfromwhere("system");
        offOnlineMessage.setMessage("text");
        offOnlineMessage.setRecode("1");
        offOnlineMessage.setDeviceType("PC");
        return null;//reponsitory.save(offOnlineMessage).toString();


    }

//@RequestParam(value="messageString",required = true) String messageString

    @RequestMapping(value = "/newoffonline", method = RequestMethod.POST, produces = "text/plan;charset=UTF-8")
    public String save(@RequestParam(value = "infos", required = true) String messageString) {
        Gson gson = new Gson();

        System.out.println("访问成功" + messageString);

        OffOnlineMessage offOnlineMessage = new OffOnlineMessage();
        Message message = gson.fromJson(messageString, Message.class);
        offOnlineMessage.setUserId(String.valueOf(message.getId()));
        offOnlineMessage.setState("0");
        offOnlineMessage.setContent(message.getContent());
        offOnlineMessage.setMessageType(message.getMessageType());
        offOnlineMessage.settowhere(message.getTowhere());
        offOnlineMessage.setfromwhere(message.getFromwhere());
        offOnlineMessage.setMessage(message.getMessage());
        offOnlineMessage.setRecode(message.getRecode());
        offOnlineMessage.setDeviceType(message.getDeviceType());
        reponsitory.save(offOnlineMessage);

        return "Success";
    }

    @RequestMapping(value = "/messagesinfo",method = RequestMethod.GET)
    public String getOffOnlineMessageInfo(){
System.out.println("success");
      //
    return "Success";//gson.toJson(messageList);
    }

    /**
     * 用于用户获取离线消息
     * @param userid
     * @return
     */
    @RequestMapping(value = "/messagetest",method = RequestMethod.POST, produces = "text/plan;charset=UTF-8")
    public String testmess(@RequestParam(value = "infos", required = true) String userid){
        List<OffOnlineMessage> messageList=reponsitory.findOfficeOnlineMessagesByTowhereAndState(userid,"0");

        return gson.toJson(messageList);
    }

    /**
     * 将未读的离线消息改为已读状态
     * @param userid
     * @return
     */
    @RequestMapping(value = "/readed",method = RequestMethod.POST, produces = "text/plan;charset=UTF-8")
    public String offonlinemessagereaded(@RequestParam(value = "infos", required = true) String userid){
        List<OffOnlineMessage> messageList=reponsitory.findOfficeOnlineMessagesByTowhereAndState(userid,"0");
       for (int i=0;i<messageList.size();i++){
           OffOnlineMessage message=messageList.get(i);
           message.setState("1");
           reponsitory.save(message);
       }

        return "success";
    }
}
